package cn.zbh.pet.service.impl;

import cn.zbh.basic.service.impl.BasicServiceImpl;

import cn.zbh.pet.domain.Pet;
import cn.zbh.pet.domain.PetType;
import cn.zbh.pet.mapper.PetTypeMapper;
import cn.zbh.pet.service.IPetService;
import cn.zbh.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PetTypeServiceImpl extends BasicServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> typeTree() {
        List<PetType> all= petTypeMapper.findAll();//查询所有部门
        HashMap<Long, PetType> map = new HashMap<>();
        for (PetType petType : all) {
            map.put(petType.getId(),petType);//将所有数据放入map中
        }
//前端需要得到的集合数据 - 部门树
        List<PetType> typeTree = new ArrayList<>();
        for (PetType petType : all) {
            if(petType.getPid()==null){//顶级部门
                typeTree.add(petType);
            }else {
                //非顶级部门
                Long pid = petType.getPid();//获取部门id
                PetType parentpetType = map.get(pid);//通过id获取 父 部门
                parentpetType.getChildren().add(petType);//

            }
        }

        return typeTree;
    }
}
